﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace T.FAS.Runtime.Base.Entity
{
    /// <summary>
    /// 变更日志
    /// </summary>
    [Serializable]
    public class TChangeLog : TRecord
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="cType">数据变更类型</param>
        /// <param name="modifier">最后修改人</param>
        /// <param name="modifyTime">最后修改时间</param>
        public TChangeLog(TDataChangeType cType,string modifier,DateTime modifyTime,string cdoCode="",string cdoName="")
        {
            this.DataChangeType = cType;
            this.Modifier = modifier;
            this.ModifyTime = modifyTime;
            this.CDOCode = CDOCode;
            this.CDOName = CDOName;
            this.ChangeDatas = new List<TChangeData>();
        }

        public TChangeData this[string columnCode]
        {
            get
            {
                return GetChangeDataByColumnCode(columnCode);
            }
        }
        private TDataChangeType _dataChangeType;
        /// <summary>
        /// 数据变更类型定
        /// </summary>
        public TDataChangeType DataChangeType
        {
            get { return _dataChangeType; }
            set
            {
                base.SetValue("DataChangeType",value);
                _dataChangeType = value;
            }
        }
        private string _modifier = string.Empty;
        /// <summary>
        /// 数据变更人
        /// </summary>
        public string Modifier
        {
            get { return _modifier; }
            set
            {
                base.SetValue("Modifier", value);
                _modifier = value;
            }
        }
        private string _modifierName = string.Empty;
        /// <summary>
        /// 数据变更人名称
        /// </summary>
        public string ModifierName
        {
            get { return _modifierName; }
            set
            {
                base.SetValue("ModifierName", value);
                _modifierName = value;
            }
        }
        private string _billID = string.Empty;
        /// <summary>
        /// 业务单据ID
        /// </summary>
        public string BillID
        {
            get { return _billID; }
            set
            {
                base.SetValue("BillID", value);
                _billID = value;
            }
        }
        private string _tenantID = string.Empty;
        /// <summary>
        /// 租户ID
        /// </summary>
        public string TenantID
        {
            get { return _tenantID; }
            set
            {
                base.SetValue("TenantID", value);
                _tenantID = value;
            }
        }

        private DateTime _modifyTime;
        /// <summary>
        /// 数据变更时间
        /// </summary>
        public DateTime ModifyTime
        {
            get { return _modifyTime; }
            set
            {
                base.SetValue("ModifyTime", value);
                _modifyTime = value;
            }
        }
        /// <summary>
        /// 变更的CDOID
        /// </summary>
        public string CDOID { get; set; }
        /// <summary>
        /// 变更的CDO编号
        /// </summary>
        public string CDOCode { get; set; }

        /// <summary>
        /// 变更的CDO名称
        /// </summary>
        public string CDOName { get; set; }
        /// <summary>
        /// 如果状态变更，记录变更前状态编号
        /// </summary>
        public string OldStateCode { get; set; }
        /// <summary>
        /// 如果状态变更，记录变更前状态名称
        /// </summary>
        public string OldStateName { get; set; }
        /// <summary>
        /// 如果状态变更，记录变更后状态编号
        /// </summary>
        public string NewStateCode { get; set; }
        /// <summary>
        /// 如果状态变更，记录变更后状态名称
        /// </summary>
        public string NewStateName { get; set; }
        private List<TChangeData> _changeDatas;
        /// <summary>
        /// 数据变更明细
        /// </summary>
        public List<TChangeData> ChangeDatas
        {
            get { return _changeDatas; }
            set
            {
                _changeDatas = value;
            }
        }

        /// <summary>
        /// 是否包含指定列的变更数据
        /// </summary>
        /// <param name="columnCode"></param>
        /// <returns></returns>
        public bool IsContain(string columnCode)
        {
            if (ChangeDatas == null || ChangeDatas.Count <= 0)
                return false;
            return ChangeDatas.Exists(x => x.ColumnCode.Equals(columnCode, StringComparison.OrdinalIgnoreCase));
        }
        /// <summary>
        /// 获取所有有变更的列编号
        /// </summary>
        /// <returns></returns>
        public List<string> GetChangedColumnCodes()
        {
            if (ChangeDatas == null || ChangeDatas.Count <= 0)
                return new List<string>();
            return ChangeDatas.Select(x => x.ColumnCode).ToList();
        }
        /// <summary>
        /// 根据列编号获取变更数据
        /// </summary>
        /// <param name="columnCode"></param>
        /// <returns></returns>
        public TChangeData GetChangeDataByColumnCode(string columnCode)
        {
            if (ChangeDatas == null || ChangeDatas.Count <= 0)
                return null;
            return ChangeDatas.Find(x => x.ColumnCode.Equals(columnCode, StringComparison.OrdinalIgnoreCase));
        }
        /// <summary>
        /// 增加变更数据
        /// </summary>
        /// <param name="data">变更数据</param>
        /// <param name="isReplaceExistData">是否要替换已存在的变更数据，如果是，进行数据替换；如果否，不进行数据添加</param>
        public void AddChangeData(TChangeData data,bool isReplaceExistData=true)
        {
            if (ChangeDatas == null)
                ChangeDatas = new List<TChangeData>();
            if (string.IsNullOrEmpty(data.ColumnCode))
                return;
            if (ChangeDatas.Exists(x => x.ColumnCode.Equals(data.ColumnCode)))
            {
                if (isReplaceExistData)
                {
                    RemoveChangeData(data.ColumnCode);
                    ChangeDatas.Add(data);
                    base.SetValue(data.ColumnCode,data);
                }
            }
            else
            {
                ChangeDatas.Add(data);
                base.SetValue(data.ColumnCode, data);
            }
        }
        /// <summary>
        /// 增加变更数据
        /// </summary>
        /// <param name="data">变更数据</param>
        /// <param name="isReplaceExistData">是否要替换已存在的变更数据，如果是，进行数据替换；如果否，不进行数据添加</param>
        public void AddRangeChangeData(List<TChangeData> datas, bool isReplaceExistData = true)
        {
            datas.ForEach(data=>
            {
                AddChangeData(data,isReplaceExistData);
            });
        }
        /// <summary>
        /// 按照列编号删除变更数据
        /// </summary>
        /// <param name="columnCode"></param>
        public void RemoveChangeData(string columnCode)
        {
            if (ChangeDatas != null)
            {
                var item = ChangeDatas.Find(x=>x.ColumnCode.Equals(columnCode));
                if (item != null)
                    ChangeDatas.Remove(item);
            }
        }

    }
    /// <summary>
    /// 数据变更类型
    /// </summary>
    public enum TDataChangeType:int
    {
        New,
        Modify,
        Delete,
    }
}
